<?php
class Add_restaurants_model extends CI_Model {

	function restaurant_exists($name)
	{
		$this->db->select('name');
		$query = $this->db->get('restaurants');
		
		foreach($query->result() as $row)
		{
			if($row->name == $name)
				return true;
		}
		return false;
	}
	
	
	function get_favorites()
	{	
		//fetch the account_id
		$this->db->where('name', $this->session->userdata('username'));
		$query = $this->db->get('accounts');
		foreach($query->result() as $row)
		{
			$account_id = $row->id;
		}
		
		
		//fetch all favorite restaurants with account_id
		$this->db->where('account_id', $account_id);
		$query = $this->db->get('favourite_restaurants');
		
		$restaurant_ids = Array();
		foreach($query->result() as $row)
		{
			$restaurant_ids[] = $row->restaurant_id;
		}
		
		
		//now fetch all the restaurant names from the restaurants list
		$data = Array('favorite_list' => Array());
		$query = $this->db->get('restaurants');
		if(count($restaurant_ids) > 0)
		{
			foreach($query->result() as $row)
			{
				foreach($restaurant_ids as $each_item)
				{
					if($row->id == $each_item)
						$data['favorite_list'][] = $row->name;
				}
			}
			return $data;
		}
		return $data = "";
	}
	
	
	function get_restaurants()
	{
		$query = $this->db->get('restaurants');
		return $query;
	}
	
	
	function add_review($data)
	{
		$insert_data = Array(
					   'restaurant_name' => $data['restaurant_name'],
    	               'message' => $data['message'],
		);
		
		$this->db->insert('review', $insert_data);
		return true;
	}
	
	function add_to_favorite($data)
	{	
		$restaurant_name = $data;
		$insert_data_favourite = Array();
			
		//fetch and set the restaurant_id
		$this->db->where('name', $restaurant_name);
		$query = $this->db->get('restaurants');
		foreach($query->result() as $row)
		{
			$insert_data_favourite['restaurant_id'] = $row->id;
		}
			
			
		//fetch and set the account_id
		$this->db->where('name', $this->session->userdata('username'));
		$query = $this->db->get('accounts');
		foreach($query->result() as $row)
		{
			$insert_data_favourite['account_id'] = $row->id;
		}
		
		
		//at last check if it is already added as a favorite
		$query = $this->db->get('favourite_restaurants');
		foreach($query->result() as $row)
		{
			if($row->account_id == $insert_data_favourite['account_id'] and $row->restaurant_id == $insert_data_favourite['restaurant_id'])
				return false;
		}
		
		$this->db->insert('favourite_restaurants', $insert_data_favourite);
		return true;
	}
	
	function edit_restaurant($data)
	{
		$insert_data = Array(
						   'name' => $data['name'],
	    	               'address_street' => $data['address_street'],
	    	               'address_zipcode' => intval($data['address_zipcode']),
	    	               'address_city' => $data['address_city'],
	    	               'opening_hours' => $data['opening_hours'],
	    	               'fastfood' => intval($data['fastfood']),
							'vegetarian' => intval($data['vegetarian']),
					    	'takeaway' => intval($data['takeaway']),
					    	'alcohol' => intval($data['alcohol']),
					    	'kitchen' => $data['kitchen']
		);
	
		$this->db->where('name', $insert_data['name']);
		$this->db->update('restaurants', $insert_data);
	}
	
	function add_restaurant($data)
	{	
		date_default_timezone_set('Europe/Prague');
		$data['date'] = date('l jS F Y H:i:s');
		
		$insert_data = Array(
    	               'name' => $data['name'],
    	               'author' => $data['author'],
    	               'date' => $data['date'],
    	               'address_street' => $data['address_street'],
    	               'address_zipcode' => intval($data['address_zipcode']),
    	               'address_city' => $data['address_city'],
    	               'opening_hours' => $data['opening_hours'],
    	               'fastfood' => intval($data['fastfood']),
						'vegetarian' => intval($data['vegetarian']),
				    	'takeaway' => intval($data['takeaway']),
				    	'alcohol' => intval($data['alcohol']),
				    	'kitchen' => $data['kitchen']
		);
		
		$this->db->insert('restaurants', $insert_data);
		//insert userID and restaurantID
		if(intval($data['favorite']) == 1)
		{
			$this->add_to_favorite($data['name']);
		}

	}
	
}
?>